// pages/login/login/login.js
const api = require('../../../api/api.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: "",
    password: "",
    isLoading: false,
    loginErrorCount: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  startLogin: function() {
    var that = this;
    that.setData({
      isLoading: true
    });
    if (that.data.password.length < 1 || that.data.username.length < 1) {
      wx.showModal({
        title: '提示',
        content: '请输入用户名或密码',
        showCancel: false
      });
      that.setData({
        isLoading: false
      });
      return false;
    }
    wx.request({
      url: api.LoginSigin,
      data: {
        username: that.data.username,
        password: that.data.password,
        state: "no",
        device: ""
      },
      method: "POST",
      header: {
        "content-type": "application/json"
      },
      success: function(res) {
        console.log(res);
        if (res.data.error_code == 0) {
          that.setData({
            "loginErrorCount": 0
          });
          app.globalData.isNeedRequest = true;
          wx.setStorage({
            key: 'token',
            data: res.data.token,
            userInfo: res.data.user,
            success: function() {
              app.globalData.isLogin = true;
              app.globalData.userInfo = res.data.user;
              app.globalData.token = res.data.token;
              app.globalData.level_id = res.data.user.level_id;
              wx.switchTab({
                url: '../../home/index/index',
              })
            }
          });
          wx.setStorage({
            key: 'userInfo',
            data: res.data.user,
          });
          wx.setStorage({
            key: 'isLogin',
            data: true,
          });
        } else if (res.data.error_code == 400) {
          var error = res.data.error_desc;
          that.setData({
            isLoading: false
          });
          wx.showToast({
            title: "账户或密码错误",
            image: "/images/error.png"
          });
          
        }
      }
    })
  },

  bindUserNameTap: function(e) {
    this.setData({
      username:e.detail.value
    });
  },
  bindPasswordTap: function(e) {
    this.setData({
      password: e.detail.value
    });
  },

  clearInput: function(e) {
    switch(e.currentTarget.id) {
      case "clear-username":
        this.setData({
          username: ""
        });
        break;
      case "clear-password":
        this.setData({
          password: ""
        });
        break;
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})